﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    #region Constants

    private const string Password = "q1w2e3";

    #endregion Constants

    protected void Page_Load(object sender, EventArgs e)
    {
        lbResult.Text = string.Empty;

        if (!IsPostBack)
        {
            //hdNonce.Value = Guid.NewGuid().ToString();
            hdNonce.Value = "3F8CB3A6-C673-4927-96B9-8BA4F2BB4177";
        }
    }

    protected void btnSendPassword_Click(object sender, EventArgs e)
    {
        string storedPassString = string.Format("{0}{1}", Password, hdNonce.Value);
        byte[] storedPassInBytes = Encoding.UTF8.GetBytes(storedPassString);

        SHA1Managed sha = new SHA1Managed();
        byte[] b0 = sha.ComputeHash(storedPassInBytes);

        StringBuilder sb = new StringBuilder();
        foreach (byte b in b0)
        {
            sb.Append(b.ToString("X2").ToLower());
        }

        string serverString = sb.ToString();
        string clientString = hdHash.Value;

        if (serverString == clientString)
        {
            lbResult.Text = "Correct";
        }
        else
        {
            lbResult.Text = "Wrong";
        }
    }
}
